//
//  SNProgressManager.m
//  MBProgressManager_test
//
//  Created by stone on 16/9/5.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNProgressManager.h"

@implementation SNProgressManager

+ (MBProgressHUD *)showToView:(UIView *)view {
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    return hud;
}

+ (MBProgressHUD *)showToView:(UIView *)view title:(NSString *)title {
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = NSLocalizedString(title, @"HUD loading title");
    return hud;
}

+ (MBProgressHUD *)showToView:(UIView *)view contentColor:(UIColor *)contentColor {
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.contentColor = contentColor;//[UIColor colorWithRed:0.f green:0.6f blue:0.7f alpha:1.f];
    
    // Set the label text.
    // hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title");
    
    return hud;
}

+ (MBProgressHUD *)showToView:(UIView *)view frame:(CGRect)frame {
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    
    hud.backgroundView.style = MBProgressHUDBackgroundStyleBlur;
    //    hud.backgroundView.color = [UIColor whiteColor];
    
    hud.backgroundView.frame = CGRectMake(frame.origin.x, 0, frame.size.width, frame.size.height+frame.origin.y);
    
    hud.frame = frame;
    
    return hud;
}

+ (MBProgressHUD *)showToView:(UIView *)view backgroundStyle:(MBProgressHUDBackgroundStyle)backgroundStyle backgroundColor:(UIColor *)backgroundColor{
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.backgroundView.style = backgroundStyle;
    hud.backgroundView.color = backgroundColor;
    return hud;
}

+ (void)showToView:(UIView *)view toast:(NSString *)toast afterDelay:(NSTimeInterval)delay {
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = NSLocalizedString(toast, @"HUD message title");
    hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
    [hud hideAnimated:YES afterDelay:delay];
}
+ (MBProgressHUD *)showToView:(UIView *)view message:(NSString *)message afterDelay:(NSTimeInterval)delay {
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = NSLocalizedString(message, @"HUD message title");
    [hud hideAnimated:YES afterDelay:delay];
    return hud;
}
+ (MBProgressHUD *)showToView:(UIView *)view frame:(CGRect)frame title:(NSString *)title contentColor:(UIColor *)contentColor backgroundStyle:(MBProgressHUDBackgroundStyle)backgroundStyle backgroundColor:(UIColor *)backgroundColor{
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    
    hud.backgroundView.style = backgroundStyle;
    hud.backgroundView.color = backgroundColor;

    hud.backgroundView.frame = CGRectMake(frame.origin.x, 0, frame.size.width, frame.size.height+frame.origin.y);
    hud.frame = frame;
    
    hud.label.text = NSLocalizedString(title, @"HUD loading title");
    hud.contentColor = contentColor;
    
    return hud;
}
+ (void)hideHUDForView:(nullable UIView *)view {
    
    [MBProgressHUD hideHUDForView:view animated:YES];
}
+ (void)hideHUD:(MBProgressHUD *)hud {
    
    [hud hideAnimated:YES];
}
+ (void)hideHUDForViewForWindow
{
    [MBProgressHUD hideHUDForView:nil animated:YES];
}
//+ (MBProgressHUD *)showToView:(UIView *)view title:(NSString *)title mode:(MBProgressHUDMode)mode progress:(NSProgress *)progress{
//
//
//    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
//
//    // Set the determinate mode to show task progress.
//    hud.mode = MBProgressHUDModeDeterminate;
//    hud.label.text = NSLocalizedString(title, @"HUD loading title");
//
//    // Set up NSPorgress
//    NSProgress * progressObject = [NSProgress progressWithTotalUnitCount:100];
//    hud.progressObject = progressObject;
//
//    // Configure a cacnel button.
//    [hud.button setTitle:NSLocalizedString(@"Cancel", @"HUD cancel button title") forState:UIControlStateNormal];
//    [hud.button addTarget:progressObject action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
//
//    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
//        // Do something useful in the background and update the HUD periodically.
//        [self doSomeWorkWithProgressObject:progressObject];
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [hud hideAnimated:YES];
//        });
//    });
//
//    return hud;
//}
//sn_note:=========  ============================ stone 🐳 ===========/
//+ (void)doSomeWorkWithProgressObject:(NSProgress *)progressObject {
//    // This just increases the progress indicator in a loop.
//    while (progressObject.fractionCompleted < 1.0f) {
//        if (progressObject.isCancelled)
//            break;
//        [progressObject becomeCurrentWithPendingUnitCount:1];
//        [progressObject resignCurrent];
//        usleep(50000);
//    }
//}
@end
